<?php
class CartModule{
	function index(){
		global $_G;
		$datas=$this->get_user_carts();
		include template('cart');
	}
	function get_user_carts(){
		global $_G;
		session_start();
		$ssid = session_id();
		$query= $_G['db']->query("SELECT c.* FROM".table('cart')."c LEFT JOIN".table('shops')."s ON c.shopid=s.shopid WHERE c.ssid='$ssid'");
		while($data=$_G['db']->fetch_array($query)){
			$carts[$data['shopid']]['shopname']=$data['shopname'];
			$carts[$data['shopid']]['cart'][$data['pid']]=$data;
		}
		return $carts;
	}
	function get_user_cart($shopid,$ssid=null){
		global $_G;
		if(!$shopid){
			error('access_deny');
		}
		global $_G;
		if(!$ssid) {
			session_start();
			$ssid = session_id();
		}
		$query = $_G['db']->query("SELECT * FROM".table('cart')."WHERE ssid='$ssid' AND shopid='$shopid'");
		while($data=$_G['db']->fetch_array($query)){
			$cart[$data['pid']]=$data;
		}
		return $cart;
		
	}
	function add(){
		global $_G;
		session_start();
		$ssid = session_id();
		$id = getGP('id','G','int');
		$good = $_G['db']->fetch('product','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
		$cart = $this->get_user_cart($good['shopid'],$ssid);
		if($good['stock'] && ($cart[$id]['qty']+1 > $good['stock'])){
			$return['s'] = false;
			$return['m'] = '';
		}elseif($good['pid']){
			if(!empty($cart[$id])){
				$_G['db']->update('cart',array('qty'=>$cart[$id]['qty']+1),array('pid'=>$id,'ssid'=>$cart[$id]['ssid'],'uid'=>$_G['uid']));
				$cart[$id]['qty'] +=1;
			}else{
				$shops = $this->getshopbyid($good['shopid']);
				$cart[$id] = $insertdata = array('uid'=>$_G['uid'],'pid'=>$good['pid'], 'ssid'=>$ssid,'shopid'=>$good['shopid'],'shopname'=>$shops['shopname'], 'price'=>$good['price'],'title'=>$good['title'],'qty'=>1,'image'=>$good['image']);
				$_G['db']->insert('cart',$insertdata);
			}
			foreach($cart as $c){
				$total += $c['qty']*$c['price'];
			}
			$return['s'] = true;
			$return['q'] = $cart[$id]['qty'];
			$return['p'] = $cart[$id]['qty'] * $cart[$id]['price'];
			$return['h'] = $cart[$id]['title'];
			$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$good['shopid']));
			$return['t'] = $total+$shop['extprice'];
				
		}
		//更新人气
		if($good['pid']){
			$_G['db']->query("UPDATE ".table('product')."SET views=views+1 WHERE pid='".$good['pid']."'");
		}
				
		print_r(json_encode($return));
		exit;
	}
	function getshopbyid($shopid){
		global $_G;
		return $_G['db']->fetch("shops","*",array('shopid'=>intval($shopid)));
	}
	function clear(){
		global $_G;
		$id = getGP('id','G','int');
		session_start();
		$ssid = session_id();
		if($id){
			$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
			$cart = $this->get_user_cart($good['shopid'],$ssid);
			$cart[$id] = array();unset($cart[$id]);
			$_G['db']->query("DELETE FROM".table('cart')."WHERE ssid='$ssid' AND pid='$id'");
			foreach($cart as $c){
				$total += $c['qty']*$c['price'];
			}
			$return['s'] = true;
			$return['q'] = 0;
			$return['p'] = 0;
			if(!$total) {
				$return['t'] = 0;
			}else{
				$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$good['shopid']));
				$return['t'] = $total+$shop['extprice'];
			}
		}else{
			$_G['db']->query("DELETE FROM".table('cart')."WHERE ssid='$ssid'");
			$return['s'] = true;
			$return['q'] = 0;
			$return['p'] = 0;
			$return['t'] = 0;
		}
		print_r(json_encode($return));
		exit;
	}
	function decrease(){
		global $_G;
		$id = getGP('id','G','int');
		session_start();
		$ssid = session_id();
		$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
		$cart = $this->get_user_cart($good['shopid'],$ssid);
		if($good['stock'] && ($cart[$id]['qty']-1 > $good['stock']) || $cart[$id]['qty'] < 2){
			$return['s'] = false;
			$return['m'] = '';
		}elseif($good['pid']){
			if(!empty($cart[$id])){
				$_G['db']->update('cart',array('qty'=>$cart[$id]['qty']-1),array('pid'=>$id,'ssid'=>$cart[$id]['ssid'],'uid'=>$_G['uid']));
				$cart[$id]['qty'] -=1;
			}
			foreach($cart as $c){
				$total += $c['qty']*$c['price'];
			}
			$return['s'] = true;
			$return['q'] = $cart[$id]['qty'];
			$return['p'] = $cart[$id]['qty'] * $cart[$id]['price'];
			$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$good['shopid']));
			$return['t'] = $total+$shop['extprice'];
		}
		print_r(json_encode($return));
		exit;
	}
	function increase(){
		global $_G;
		$id = getGP('id','G','int');
		session_start();
		$ssid = session_id();
		$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
		$cart = $this->get_user_cart($good['shopid'],$ssid);
		if($good['stock'] && ($cart[$id]['qty']+1 > $good['stock'])){
			$return['s'] = false;
			$return['m'] = '';
		}elseif($good['pid']){
			if(!empty($cart[$id])){
				$_G['db']->update('cart',array('qty'=>$cart[$id]['qty']+1),array('pid'=>$id,'ssid'=>$cart[$id]['ssid'],'uid'=>$_G['uid']));
				$cart[$id]['qty'] +=1;
			}
			foreach($cart as $c){
				$total += $c['qty']*$c['price'];
			}
			$return['s'] = true;
			$return['q'] = $cart[$id]['qty'];
			$return['p'] = $cart[$id]['qty'] * $cart[$id]['price'];
			$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$good['shopid']));
			$return['t'] = $total+$shop['extprice'];
		}
		print_r(json_encode($return));
		exit;
	}
	function checkout(){
		//结算前ajax检测数量?或者结算程序里判断
		if(!IS_POST)exit;
		global $_G;
		session_start();
		$ssid = session_id();
		$phone = getGP('phone','P');
		$addr = getGP('address','P');
		$dtime = getGP('deliver_time','P');
		$extra = getGP('note','P');
		$shopid = getGP('shopid','P');
		if(!$shopid){
			error('参数错误,请联系客服');
		}
		if(!is_phone($phone)){
			error('手机号错误');
		}
		if(strlen($addr)<10){
			error('地址错误');
		}
		$carts = $this->get_user_cart($shopid,$ssid);
		foreach($carts as $c){
			$total[$c['shopid']] += $c['qty']*$c['price'];
		}
		//检测库存？
		//按店铺区分
		foreach($total as $k => $v) {
			$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$k));
			$txn = array(
				'uid'=>$_G['uid'],
				'touid' => $k,
				'type'=>'food',
				'shipping_cost' => $shop['extprice'],
				'payment_type' => 'cod',
				'gross' => $v,
				'created' => TIMESTAMP,
			);
			if($v < $shop['minprice']){
				continue;
			}
			$txn['gross'] +=$shop['extprice'];
			$txn['txnid']=$_G['db']->insert("orders",$txn);
			foreach($carts as $c){
				if($c['shopid']!=$k)continue;
				$food = array(
					'txnid' => $txn['txnid'],
					'pid' => $c['pid'],
					'title' => $c['title'],
					'price' => $c['price'],
					'qty' => $c['qty'],
					'data' => '',
				
				);
				$_G['db']->insert("orders_product",$food);
			}
			$addr = array(
				'txnid' =>$txn['txnid'],
				'phone'=> $phone,
				'street' => $addr,
				'shipping_fee'=> '0',
				'shipping_time' => $dtime,
				'shipping_note' => $extra,
				
			);
			$_G['db']->insert('orders_shipping',$addr);
			$orderid .=$$txn['txnid'].', ';
			$_G['db']->query("DELETE FROM".table('cart')."WHERE ssid='$ssid' AND shopid='$k'");
		}
		error('下单成功，请及时关注你的 <a href="'.url('home.php?m=myorder').'">订单状态</a>','',null,false);
	}
	//结算前ajax检测
	function ajaxcheck(){
		global $_G;
		$shopid = getGP('id','G');
		$carts = $this->get_user_cart($shopid,$ssid);
		foreach($carts as $c){
			$total +=$c['qty']*$c['price'];
			$num +=$c['qty'];
		}
		$return['s'] = true;
		$return['e'] = '';
		$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$shopid));
		if($total < $shop['minprice']){
			$return['s'] = false;
			$return['e'] = '您所点的菜没有达到起价(起送价 '.$shop['minprice'].' 元)';
			
		}
		if($num < $shop['minnum']){
			$return['s'] = false;
			$return['e'] = '您需要点足 '.$shop['minnum'].' 份才能下单)';
		}
		print_r(json_encode($return));
		exit;
	}
}	